#Fichero: Makefile

##############################
###### CONFIGURACION #########
##############################

#Posibles: IPHONE, SIMULATOR, LOCAL
PLATFORM = IPHONE

#version del SDK del iPhone
VERSION = 3.0

ifeq ($(PLATFORM),SIMULATOR)
ICETOUCH_HOME = /Developer/SDKs/IceTouch-1.0/iphonesimulator.sdk
endif

ifeq ($(PLATFORM),LOCAL)
ICETOUCH_HOME = /Developer/SDKs/IceTouch-1.0/macosx.sdk
endif

ifeq ($(PLATFORM),IPHONE)
ICETOUCH_HOME = /Developer/SDKs/IceTouch-1.0/iphoneos.sdk
endif

BIN = ./bin
LIB = ./dist/lib
LIB2 = $(ICETOUCH_HOME)/usr/local/lib/

ifeq ($(PLATFORM),LOCAL)
LIB2 = /opt/IceTouch-1.0/lib
endif

ifeq ($(PLATFORM),SIMULATOR)
LIB2 = /opt/IceTouch-1.0/lib
endif

OBJ = ./obj

DEFS = ./ICE_defs
SKELS = ./ICE_skel

MAIN = .
INIT = ./initialize
AUXILIARY = ./auxiliary
EVENTS = ./events
DEFAULTS = $(EVENTS)/defaults
MYSQL = /usr/local/mysql

INCLUDE = -I$(SKELS)/ -I$(INIT)/ -I$(META_UTIL)/ -I$(AUXILIARY) -I$(EVENTS)/ -I$(MYSQL)/include/ -I$(MAIN)/ -I$(DEFAULTS)
SRC_COMPILER = g++-4.0
LD = ld
AR = ar
IDL_COMPILER = /Developer/SDKs/IceTouch-1.0/bin/slice2objc
IDL_OPTS = -I$(DEFS)/ --output-dir $(SKELS)/

CXX_OPTS_MYSQL = -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT
CXX_LIB_PATHS = -L$(MYSQL)/lib/ -L$(LIB) -L$(LIB2)
CXX_OMNI_LIBS = -lIceObjC -framework Foundation
CXX_LIBS = $(CXX_LIB_PATHS) $(CXX_OMNI_LIBS) -lmysqlclient -lz -lm -lmygcc

ifeq ($(PLATFORM),LOCAL)
CXX_OMNI_LIBS += -lssl -lcrypto -lbz2 -liconv
endif

ifeq ($(PLATFORM),SIMULATOR)
SRC_COMPILER = /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.0
LD = /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ld
AR = /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/ar
CXX_OPTS = -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator$(VERSION).sdk
CXX_OPTS += -arch i386 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks \
          -O0 -Wreturn-type -Wunused-variable -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000 \
          -mmacosx-version-min=10.5 -gdwarf-2
endif

ifeq ($(PLATFORM),IPHONE)
SRC_COMPILER = /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin9-g++-4.2.1
LD = /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ld
AR = /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar
CXX_OPTS = -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS$(VERSION).sdk
CXX_OPTS += -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks \
          -O0 -Wreturn-type -Wunused-variable -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000 -fmessage-length=0 -miphoneos-version-min=$(VERSION) \
          -gdwarf-2 -mthumb
CXX_OMNI_LIBS += -framework CFNetwork -framework Security
endif

ifeq ($(PLATFORM),LOCAL)
CXX_OPTS = -isysroot /Developer/SDKs/MacOSX10.5.sdk/ -gdwarf-2 -O2 -fno-common -fPIC -Wall -gdwarf-2 -bind_at_load -D_REENTRANT -D__OSVERSION__=1 -D__darwin__ -D__x86__ -fno-inline
endif

INCLUDE += -I$(ICETOUCH_HOME)/usr/local/include/

LINKOPTS = rcs
DYNLINKOPTS = -dynamiclib -install_name @executable_path/Frameworks/libCollaboration.dylib
DYNLINKIMPORTS = /usr/lib/bundle1.o

ifeq ($(PLATFORM),IPHONE)
DYNLINKOPTS += -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS$(VERSION).sdk -L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/lib
endif

ifeq ($(PLATFORM),IPHONE)
PLAT_NAME = iphoneos
endif

ifeq ($(PLATFORM),SIMULATOR)
PLAT_NAME = simulator
endif

ifeq ($(PLATFORM),LOCAL)
PLAT_NAME = macosx
endif

OBJS_SKEL = $(OBJ)/collaboration.o

OBJS_EVENTS = $(OBJ)/event.o $(OBJ)/eventConsumer.o $(OBJ)/eventSupplier.o $(OBJ)/eventHandler.o $(OBJ)/listeningObject.o

OBJS_INIT = $(OBJ)/coordinationMiddleware.o
OBJS_DEFAULTS = $(OBJ)/pairEvent.o $(OBJ)/drawingEvent.o $(OBJ)/drawingListener.o

OBJS_MAIN_TEST = $(OBJ)/test_client.o

OBJS = $(OBJS_SKEL) $(OBJS_EVENTS) $(OBJS_INIT) $(OBJS_DEFAULTS)

.PHONY = all clean dist

##############################
##### DESTINOS VIRTUALES #####
##############################


#para el simulador y el iphone solo se generan las bibliotecas

ifeq ($(PLATFORM),SIMULATOR)
all: $(LIB)/libCollaboration.dylib $(LIB)/libCollaborationStatic.a
endif

ifeq ($(PLATFORM),IPHONE)
all: $(LIB)/libCollaboration.dylib $(LIB)/libCollaborationStatic.a
endif

ifeq ($(PLATFORM),LOCAL)
#una vez terminada la transicion a objc, agregar $(BIN)/MIService
all: $(LIB)/libCollaboration.dylib $(LIB)/libCollaborationStatic.a $(BIN)/test_client
endif

dist: $(LIB)/libCollaboration.dylib $(LIB)/libCollaborationStatic.a
	cp  $(SKELS)/*.h $(EVENTS)/*.h $(INIT)/*.h $(DEFAULTS)/*.h dist/$(PLAT_NAME)/include
	cp  $(LIB2)/* dist/$(PLAT_NAME)/lib

clean:
	rm -f $(OBJ)/*
	rm -f dist/$(PLAT_NAME)/lib/libCollaboration.dylib dist/$(PLAT_NAME)/lib/libCollaborationStatic.a
	rm -f $(BIN)/test_client

#############################
######  BIBLIOTECAS  ########
#############################

$(LIB)/libCollaboration.dylib : $(OBJS)
	$(SRC_COMPILER) $(DYNLINKOPTS) -o dist/$(PLAT_NAME)/lib/libCollaboration.dylib $(OBJS) $(CXX_OMNI_LIBS) $(CXX_LIB_PATHS)

$(LIB)/libCollaborationStatic.a : $(OBJS)
	$(AR) $(LINKOPTS) dist/$(PLAT_NAME)/lib/libCollaborationStatic.a $(OBJS)

#############################
###### EJECUTABLES ##########
#############################

$(BIN)/test_client : $(OBJS_MAIN_TEST) $(LIB)/libCollaborationStatic.a
	$(SRC_COMPILER) -o $(BIN)/test_client $(OBJS_MAIN_TEST) -L$(LIB)/ -lCollaborationStatic $(CXX_OPTS) $(CXX_OMNI_LIBS) -L$(LIB2)

##############################
######   ICE_SKEL   ########
##############################

$(SKELS)/collaboration.h : $(DEFS)/collaboration.ice
	$(IDL_COMPILER) $(IDL_OPTS) $(DEFS)/collaboration.ice

$(OBJ)/collaboration.o : $(SKELS)/collaboration.h $(SKELS)/collaboration.m
	$(SRC_COMPILER) -c $(SKELS)/collaboration.m -o $(OBJ)/collaboration.o $(CXX_OPTS) $(INCLUDE)


#############################
#######   EVENTS     ########
#############################

$(OBJ)/event.o : $(EVENTS)/event.m $(EVENTS)/event.h
	$(SRC_COMPILER) -c $(EVENTS)/event.m -o $(OBJ)/event.o $(CXX_OPTS) $(INCLUDE)

$(OBJ)/eventConsumer.o : $(EVENTS)/eventConsumer.m $(EVENTS)/eventConsumer.h
	$(SRC_COMPILER) -c $(EVENTS)/eventConsumer.m -o $(OBJ)/eventConsumer.o $(CXX_OPTS) $(INCLUDE)

$(OBJ)/eventSupplier.o : $(EVENTS)/eventSupplier.m $(EVENTS)/eventSupplier.h
	$(SRC_COMPILER) -c $(EVENTS)/eventSupplier.m -o $(OBJ)/eventSupplier.o $(CXX_OPTS) $(INCLUDE)

$(OBJ)/eventHandler.o : $(EVENTS)/eventHandler.m $(EVENTS)/eventHandler.h
	$(SRC_COMPILER) -c $(EVENTS)/eventHandler.m -o $(OBJ)/eventHandler.o $(CXX_OPTS) $(INCLUDE)

$(OBJ)/listeningObject.o : $(EVENTS)/listeningObject.m $(EVENTS)/listeningObject.h
	$(SRC_COMPILER) -c $(EVENTS)/listeningObject.m -o $(OBJ)/listeningObject.o $(CXX_OPTS) $(INCLUDE)


##############################
######      INIT       #######
##############################

$(OBJ)/coordinationMiddleware.o : $(INIT)/coordinationMiddleware.m $(INIT)/coordinationMiddleware.h
	$(SRC_COMPILER) -c $(INIT)/coordinationMiddleware.m -o $(OBJ)/coordinationMiddleware.o $(CXX_OPTS) $(CXX_OPTS_MYSQL) $(INCLUDE)

##############################
###         DEFAULTS        ##
##############################

$(OBJ)/pairEvent.o : $(DEFAULTS)/pairEvent.m $(DEFAULTS)/pairEvent.h
	$(SRC_COMPILER) -c $(DEFAULTS)/pairEvent.m -o $(OBJ)/pairEvent.o $(CXX_OPTS) $(INCLUDE)

$(OBJ)/drawingEvent.o : $(DEFAULTS)/drawingEvent.m $(DEFAULTS)/drawingEvent.h
	$(SRC_COMPILER) -c $(DEFAULTS)/drawingEvent.m -o $(OBJ)/drawingEvent.o $(CXX_OPTS) $(INCLUDE)

$(OBJ)/drawingListener.o : $(DEFAULTS)/drawingListener.m $(DEFAULTS)/drawingListener.h
	$(SRC_COMPILER) -c $(DEFAULTS)/drawingListener.m -o $(OBJ)/drawingListener.o $(CXX_OPTS) $(INCLUDE)


##############################
#######     MAIN       #######
##############################

$(OBJ)/test_client.o : $(MAIN)/test_client.m
	$(SRC_COMPILER) -c $(MAIN)/test_client.m -o $(OBJ)/test_client.o $(CXX_OPTS) $(INCLUDE)

